<?php
require_once 'Zend/Db/Tree/Node/ContainerAbstract.php';

class Zend_Db_Tree_Node extends Zend_Db_Tree_Node_ContainerAbstract
{
    protected $_treeAdapter;

    protected $_data = array();

    protected $_pk;

    public function __construct(array $options = array())
    {
        parent::__construct($options);
        if (isset($options['data'])) {
            $this->_data = $options['data'];
        }
        $info = $this->_table->info();
        $this->_pk = reset($info);
    }

    public function setData(array $data)
    {
        $data = array_intersect_key($data, $this->_data);
        foreach ($data as $name => $value) {
            $this->__set($name, $value);
        }
        return $this;
    }

    public function __get($name)
    {
        $name = $this->_transformProperty($name);
        if (!array_key_exists($name, $this->_data)) {
            require_once 'Zend/Db/Tree/Node/Exception.php';
            throw new Zend_Db_Tree_Node_Exception("Specified property '$name' is not in the node");
        }
        return $this->_data[$name];
    }

    public function __set($name, $value)
    {
        $name = $this->_transformProperty($name);
        if (!array_key_exists($name, $this->_data)) {
            require_once 'Zend/Db/Tree/Node/Exception.php';
            throw new Zend_Db_Tree_Node_Exception("Specified property '$name' is not in the node");
        }
        $this->_data[$name] = $value;
    }

    public function __isset($name)
    {
        $name = $this->_transformProperty($name);
        return array_key_exists($name, $this->_data);
    }

    public function isEmpty()
    {
        return $this->_getPrimaryKey() === null;
    }

    protected function _getPrimaryKey()
    {
        return $this->_pk;
    }

    protected function _transformProperty($name)
    {
        return $name;
    }
}
